from pathlib import Path
import datetime


def strf_time(timestamp):
    format_string = "%Y-%m-%d %H:%M:%S"
    dt_object = datetime.datetime.fromtimestamp(timestamp)
    formatted_time = dt_object.strftime(format_string)
    return formatted_time


class FileInformation:
    '''
    文件信息
    '''

    def __init__(self, filename):
        self.file = Path(filename)
        self.stat = self.file.stat()

    def size(self) -> str:
        """
        文件大小
        """
        mb = round(self.stat.st_size / 1024, 2)
        return str(mb) + 'KB'

    def st_ctime(self) -> str:
        """
        创建时间
        """
        return strf_time(self.stat.st_ctime)

    def st_mtime(self) -> str:
        """
        上次修改时间
        """
        return strf_time(self.stat.st_mtime)

    def st_atime(self) -> str:
        """
        访问时间
        """
        timestamp = self.stat.st_atime
        return strf_time(timestamp)

    def name(self) -> str:
        """
        文件名
        """
        return self.file.name

    def stem(self) -> str:
        """
        文件前缀
        """
        return self.file.stem

    def suffix(self) -> str:
        """
        文件后缀
        """
        return self.file.suffix

    def parent(self) -> Path:
        """
        文件父级
        """
        return self.file.resolve().parent

    def home(self) -> Path:
        """
        用户home路径
        """
        return self.file.home()


if __name__ == '__main__':
    filename = r"C:\Users\Administrator\Desktop\新建文件夹\网签系统数据入库流程.docx"
    fp = FileInformation(filename)
    print(fp.file.home())
